package infostroy.bookagolf.components.inputs;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

/**
 * Created by valer_000 on 3/1/14.
 */
public class GolfDecimalTextField extends GolfTextField {

    @Override
    protected Document createDefaultModel() {
        return new DecimalDocument();
    }

    private class DecimalDocument extends PlainDocument {
        private static final String format = ".1234567890";

        @Override
        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
            boolean insert = true;
            for (String character : str.split("")) {
                if (!format.contains(character)) {
                    insert = false;
                    break;
                }
            }
            if (insert) {
                super.insertString(offs, str, a);
            }
        }
    }
}
